/*-- $Id: Weather.java 4 2005-11-28 19:22:36Z sanvekar $ --*/
package com.aurigalogic.weatherPortlet;

import java.util.List;

import org.apache.commons.digester.*;

/**
 * @author Sonal Anvekar
 * @version $Revision: 4 $ $Date: 2005-11-29 00:52:36 +0530 (Tue, 29 Nov 2005) $ 
 */
public class Weather implements java.io.Serializable {

	private static Digester digester = null;
	private String tempUnit = null;
	private String volumeUnit = null;
	private String pressureUnit = null;
	private String speedUnit = null;
	private String location = null;
	private String lastUpdated = null;
	private List days = new java.util.ArrayList();

	public static Weather parse(final String xml) {
		Digester _digester = getDigester();
		try {
			return (Weather) _digester.parse(new java.io.StringReader(xml));
		} catch (Exception e) {
			throw new SysException();
		}

	}

	public String getTempUnit() {
		return tempUnit;
	}

	public void setTempUnit(final String tempUnit) {
		this.tempUnit = tempUnit;
	}

	public String getSpeedUnit() {
		return speedUnit;
	}

	public void setSpeedUnit(final String speedUnit) {
		this.speedUnit = speedUnit;
	}

	public String getLastUpdated() {
		return lastUpdated;
	}

	public void setLastUpdated(final String lastUpdated) {
		this.lastUpdated = lastUpdated;
	}

	public String getVolumeUnit() {
		return volumeUnit;
	}

	public void setVolumeUnit(final String volumeUnit) {
		this.volumeUnit = volumeUnit;
	}

	public String getPressureUnit() {
		return pressureUnit;
	}
		
	public void setPressureUnit(final String pressureUnit) {
		this.pressureUnit = pressureUnit;
	}
	
	public String getLocation() {
		return location;
	}
		
	public void setLocation(final String location) {
		this.location = location;
	}
	
	public List getDays() {
		return days;
	}
		
	public void addDay(final Day day) {
		days.add(day);
	}

	private static Digester getDigester() {
		if (digester == null) {
			digester = new Digester();
			digester.setValidating(false);
			digester.addObjectCreate("weather", Weather.class);
			digester.addBeanPropertySetter("weather/head/ut", "tempUnit");
			digester.addBeanPropertySetter("weather/head/up", "pressureUnit");
			digester.addBeanPropertySetter("weather/head/ur", "volumeUnit");
			digester.addBeanPropertySetter("weather/head/us", "speedUnit");
			digester.addBeanPropertySetter("weather/loc/dnam", "location");
			digester.addBeanPropertySetter("weather/dayf/lsup", "lastUpdated");

			digester.addObjectCreate("weather/dayf/day", Day.class);
			digester.addSetProperties("weather/dayf/day", new String[] {"t", "dt"}, new String[] {"dow", "date"});
			digester.addSetNext("weather/dayf/day", "addDay");
			digester.addBeanPropertySetter("weather/dayf/day/hi");
			digester.addBeanPropertySetter("weather/dayf/day/low");
			digester.addBeanPropertySetter("weather/dayf/day/sunrise");
			digester.addBeanPropertySetter("weather/dayf/day/sunset");
	
			digester.addObjectCreate("weather/dayf/day/part", DayPart.class);
			digester.addSetProperties("weather/dayf/day/part", new String[] {"p"}, new String[] {"type"});
			digester.addBeanPropertySetter("weather/dayf/day/part/icon");
			digester.addBeanPropertySetter("weather/dayf/day/part/t", "description");
			digester.addBeanPropertySetter("weather/dayf/day/part/ppcp");
			digester.addBeanPropertySetter("weather/dayf/day/part/hmid", "humidity");
			digester.addSetNext("weather/dayf/day/part", "addPart");

			digester.addObjectCreate("weather/dayf/day/part/wind", Wind.class);
			digester.addSetNext("weather/dayf/day/part/wind", "setWind");
			digester.addBeanPropertySetter("weather/dayf/day/part/wind/gust");
			digester.addBeanPropertySetter("weather/dayf/day/part/wind/t", "direction");
			digester.addBeanPropertySetter("weather/dayf/day/part/wind/d", "degree");
			digester.addBeanPropertySetter("weather/dayf/day/part/wind/s", "speed");
		}
		return digester;	
	}

}
